# int INTEGER=50,KEYWORD=100;

def CreateNewSymTableNode():
      global HashSize
      t = SymTableNode()
      t.scope = 0
      t.next = None
      for i in range(HashSize):
	    t.table[i] = None
      return t

def lookup(name, FuncName):
      r = SymEntry()
      s = SymEntry()
      r = None      # Check original code as r stands for both pointer and symentry
      temp = SymTableNode()
      if name != None:
	    a = 0
	    i = 0
	    k = name[0]%HashSize
	    for a in range(FuncName):
		  temp = global1[a]
		  while temp.next != None:
			s = temp.table[k]
			if s!= None:
			      if s.id == name:
				    r = s
			      else:
				    while s.next != None:
					  s = s.next
					  if s.id == name:
						r = s
						break
			temp = temp.next
		  s = temp.table[k]			
		  if s!=None:
			if s.id == name:
			      r = s
			else:
			      while s.next != None:
				    s = s.next
				    if s.id == name:
					  r = s
					  break;
      return r


def InsertSymTable(name,currentScope,typ,FuncName):
      if tail[FuncName].scope!=currentScope:
	    #printf("NewScope : %d.\n",currentScope);
	    temp = SymTableNode()
	    temp = CreateNewSymTableNode()
	    temp.scope = currentScope
	    tail[FuncName].next=temp
	    tail[FuncName]=temp
      
      p = SymEntry()
      p.id = name
      p.scope=tail[FuncName].scope
      p.type = typ
      p.next=tail[FuncName].table[name[0]%HashSize]
      tail[FuncName].table[name[0]%HashSize]=p
      return p

def InitializeSymTable(KEYWORD):
      counter=0
      global1[0]=CreateNewSymTableNode()
      tail[0]=global1[0]
      global1[1]=CreateNewSymTableNode()
      tail[1]=global1[1]
      #global[1]->name=(char*)malloc(sizeof(char)*100);//Function name is assumed to be less than 100 for now.
      globarl[0].name = "Global"
      p = SymEntry()    
      print "Global created.\n"
      p = InsertSymTable("auto",0,KEYWORD,0)
      p.keywordval=AUTO
      #printf("Inserted auto,name : %s , value : %d\n",p->id,p->keywordval);
      p = InsertSymTable("break",0,KEYWORD,0)
      p.keywordval=BREAK
      p=InsertSymTable("case",0,KEYWORD,0)
      p.keywordval=CASE
      p=InsertSymTable("char",0,KEYWORD,0)
      p.keywordval=CHAR
      p=InsertSymTable("const",0,KEYWORD,0)
      p.keywordval=CONST
      p=InsertSymTable("continue",0,KEYWORD,0)
      p.keywordval=CONTINUE
      p=InsertSymTable("default",0,KEYWORD,0)
      p.keywordval=DEFAULT
      p=InsertSymTable("do",0,KEYWORD,0)
      p.keywordval=DO
      p=InsertSymTable("double",0,KEYWORD,0)
      p.keywordval=DOUBLE
      p=InsertSymTable("else",0,KEYWORD,0)
      p.keywordval=ELSE
      p=InsertSymTable("enum",0,KEYWORD,0)
      p.keywordval=ENUM
      p=InsertSymTable("extern",0,KEYWORD,0)
      p.keywordval=EXTERN
      p=InsertSymTable("float",0,KEYWORD,0)
      p.keywordval=FLOAT
      p=InsertSymTable("for",0,KEYWORD,0)
      p.keywordval=FOR
      p=InsertSymTable("goto",0,KEYWORD,0)
      p.keywordval=GOTO
      p=InsertSymTable("if",0,KEYWORD,0)
      p.keywordval=IF
      p=InsertSymTable("int",0,KEYWORD,0)
      p.keywordval=INT
      p=InsertSymTable("long",0,KEYWORD,0)
      p.keywordval=LONG
      p=InsertSymTable("register",0,KEYWORD,0)
      p.keywordval=REGISTER
      p=InsertSymTable("return",0,KEYWORD,0)
      p.keywordval=RETURN
      p=InsertSymTable("short",0,KEYWORD,0)
      p.keywordval=SHORT
      p=InsertSymTable("signed",0,KEYWORD,0)
      p.keywordval=SIGNED
      p=InsertSymTable("sizeof",0,KEYWORD,0)
      p.keywordval=SIZEOF
      p=InsertSymTable("static",0,KEYWORD,0)
      p.keywordval=STATIC
      p=InsertSymTable("struct",0,KEYWORD,0)
      p.keywordval=STRUCT
      p=InsertSymTable("switch",0,KEYWORD,0)
      p.keywordval=SWITCH
      p=InsertSymTable("typedef",0,KEYWORD,0)
      p.keywordval=TYPEDEF
      p=InsertSymTable("union",0,KEYWORD,0)
      p.keywordval=UNION
      p=InsertSymTable("unsigned",0,KEYWORD,0)
      p.keywordval=UNSIGNED
      p=InsertSymTable("void",0,KEYWORD,0)
      p.keywordval=VOID
      p=InsertSymTable("volatile",0,KEYWORD,0)
      p.keywordval=VOLATILE
      p=InsertSymTable("while",0,KEYWORD,0)
      p.keywordval=WHILE
      p=InsertSymTable("print_t",0,KEYWORD,0)
      p.keywordval=0
      p.isFunction=1
      p.linenum=0
      p.type=400
      p.scope=0				             
      p.keywordval=0	#UNION, INT , etc.
      p.isPointer=0
      p.isArray=0
      p.ArrayLimit=0      
      p.offset=-1
      p.width=0
      p.numParameters=1
      p.ParameterList[0] = initAttr(p.ParameterList[0])
      p.ParameterList[0].type=400

      for counter in  range(MaxParcounter):
	    p.ParameterList[counter]=None


def PrintSymTable():
      print "\nPrinting Symbol Table :\n"
      s = SymEntry();
      temp = SymTableNode()
      i = 0
      k = 0	#//Biggest Scope found less than or equal to internalScope.
      for k in range(2):	
	    print "\nLooking in Function Node ",k,"\n"
	    temp = global1[k]
	    while temp.next != None:
		  print "\nLooking in Node %d\n",temp.scope		 
		  for i in range(HashSize):
			s = temp.table[i]
			if s!= None:
			      PrintSymEntry(s)
			      while s.next != None:
				    s = s.next
				    PrintSymEntry(s)
			#printf("\n00000000000000000000000000000000000000000\n");
		  temp=temp.next
	    print "\nLooking in Node ",temp.scope,"\n"
	    for i in  range(HashSize):
		  s = temp.table[i]
		  if s!= None:
			PrintSymEntry(s)
			while s.next != None:
			      s = s.next
			      PrintSymEntry(s)
      #printf("\nSearch Finished\n");  


def PopSym(FuncName):
      temp = SymTableNode()
      temp=global1[FuncName]
      while temp.next.next != None:
	    temp = temp.next
      temp.next = None
      free(tail[FuncName])
      tail[FuncName]=temp
      
'''      
/*void PopSym()
{
  SymTableNode* temp=global;
  while(temp->next->!=NULL)
  {
      temp=temp->next;
  }
  temp->next=NULL;
  free(tail);
  tail=temp;
}*/

/*int main()
{
  printf("You are in.\n");
  InitializeSymTable();  
  printf("Global Node bana.\n");
  InsertSymTable("ABC1",1,1111);//INTEGER);
  printf("ABC1hmm inserted.\n");
  InsertSymTable("ABC2",1,2222);//FLOAT,(void *)"10.910");
  printf("ABC2 inserted.\n");
  InsertSymTable("BBC3",2,3333);//"POINTER TO CHAR",(void *)"asdad");
  printf("BBC3 inserted.\n");
  struct SymEntry *p;
  p=lookup("BBC3");  
  //p=global->next->table[2];
  printf("Searched for BBC3.Tail ka scope : %d\n",tail->scope);
  if(p==NULL)
    printf("Variable BBC3 not found.\n");
  else printf("Variable found.Name : %s, Type : %d , Scope : %d, Value : %d\n",p->id,p->type,p->scope,p->keywordval);
}*/
'''